package com.jiaquyun.jcyx.main.mine

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ActivityUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.jetpack.mode.AddressViewModel
import com.module.lemlin.LoadingDialog
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_address.*


class MineAddressActivity : OwnerViewModeActivity<AddressViewModel>() {

    companion object {
        const val KEY_ADDRESS_ACTION = "KEY_ADDRESS_ACTION"
        fun open(activity: Activity, action: Int? = 0) {
            val bundle = Bundle().apply {
                putInt(KEY_ADDRESS_ACTION, action ?: 0)
            }
            ActivityUtils.startActivityForResult(
                bundle, activity, MineAddressActivity::class.java, (1)
            )
        }
    }

    private val mAction: Int
        get() = intent.extras?.getInt(KEY_ADDRESS_ACTION) ?: 0

    override fun viewModel(): AddressViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_address

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, view = flMineAddressBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { finish() }

        supportFragmentManager(AddressListFragment.newInstance(mAction))
    }

    override fun initData() {
        viewModel.userAddressList()
    }

    override fun initViewMode() {

        viewModel.addressDetailLiveData.observe(this as LifecycleOwner, Observer { response ->
            responseStatus(response)
            if (response.status != Status.SUCCESS)
                return@Observer
            val data = response.data?.data
                ?: return@Observer
            supportFragmentManager(AddressEditFragment.newInstance(data), (true))
        })

        viewModel.addressDeleteLiveData.observe(this as LifecycleOwner, Observer { response ->
            responseStatus(response)
            if (response.status != Status.SUCCESS)
                return@Observer
            onBackPressed()
        })

        viewModel.addressSaveLiveData.observe(this as LifecycleOwner, Observer { response ->
            responseStatus(response)
            if (response.status != Status.SUCCESS)
                return@Observer
            onBackPressed()
        })
    }

    private fun responseStatus(response: HttpResponseBody<*>) {
        when (response.status) {
            Status.START -> {
                LoadingDialog.show(this as Context, resId = R.layout.dialog_load_style_1)
            }
            Status.FAILURE -> {
                LoadingDialog.dismiss()
                toast(response.error?.msg)
            }
            Status.SUCCESS -> {
                LoadingDialog.dismiss()
            }
        }
    }


    fun onClick(view: View) {
        if (view.id == R.id.tvAddressListAdd) {
            supportFragmentManager(AddressEditFragment.newInstance(), (true))
        }
    }

    private fun supportFragmentManager(fragment: Fragment, isToBackStack: Boolean = false) {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        if (isToBackStack) {
            transaction.setCustomAnimations(
                R.animator.slide_right_in, R.animator.slide_left_out,
                R.animator.slide_left_in, R.animator.slide_right_out
            )
            transaction.replace(R.id.flMineAddressBody, fragment)
            transaction.addToBackStack(null)
        } else {
            transaction.add(R.id.flMineAddressBody, fragment)
        }
        transaction.commit()
    }

}

